package com.owen.liferay.jdo.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import javax.jdo.Transaction;
import javax.jdo.annotations.*;

import com.google.web.bindery.requestfactory.server.RequestFactoryServlet;

@PersistenceCapable(detachable="true")
@Version(column = "version", strategy = VersionStrategy.DATE_TIME, extensions = { @Extension(vendorName = "DataNucleus", 
	key = "field-name", value = "version") })
public class Class {
	
	final static PersistenceManagerFactory PMF = JDOHelper.getPersistenceManagerFactory("datanucleus.properties");

	@Persistent(valueStrategy = IdGeneratorStrategy.INCREMENT)
	@PrimaryKey
	private long id;

	@Persistent
	private String classId = null;

	@Persistent
	private String className = null;

	@Persistent
	private Date version;

	public Class() {
	};

	public Class(String classId, String className) {
		super();
		this.classId = classId;
		this.className = className;
	}

	public Date getVersion() {
		if (version == null)
			version = new Date();
		return version;
	}

	public void setVersion(Date version) {
		this.version = version;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	// server methods

	public String getClassId() {
		return classId;
	}

	public void setClassId(String classId) {
		this.classId = classId;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public void persist() {
		PersistenceManager pm = PMF.getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		
		try {
			tx.begin();
			
			pm.makePersistent(this);

			tx.commit();
			
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
			pm.close();
		}
	}

	public void remove() {
		PersistenceManager pm = PMF.getPersistenceManager();
		Transaction tx = pm.currentTransaction();

		try {
			tx.begin();

			pm.deletePersistent(this);

			tx.commit();

		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
			pm.close();
		}
	}
	
	public static void deleteRecord(long id) {
		System.out.println("Removing record with id: " + id);

		PersistenceManager pm = PMF.getPersistenceManager();

		Transaction tx = pm.currentTransaction();

		try {
			tx.begin();

			Class book = pm.getObjectById(Class.class, id);
			
			pm.deletePersistent(book);

			tx.commit();
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
			pm.close();
		}

	}
	
	public static List<Class> returnAllClasses() {
		
		PersistenceManager pm = PMF.getPersistenceManager();
				
		Transaction tx = pm.currentTransaction();
		List<Class> classesReturned = null;
		List<Class> classesDetached = new ArrayList<Class>();
		try {
			tx.begin();

			Query q = pm.newQuery("SELECT FROM " + Class.class.getName());

			classesReturned = (List<Class>) q.execute();

			Iterator<Class> iter = classesReturned.iterator(); 
			
			System.out.println("query completed, classes returned are: " + classesReturned.size());
			
			while (iter.hasNext()) {
				Class book = pm.detachCopy(iter.next());
				classesDetached.add(book);
			}
			
			tx.commit();
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
			pm.close();
		}

		return classesDetached;
	}

	public static Class findClass(long id) {
		
		PersistenceManager pm = PMF.getPersistenceManager();

		Transaction tx = pm.currentTransaction();

		Class book = null;
		
		Class detached = null;

		try {
			tx.begin();

			book = pm.getObjectById(Class.class, id);
			
			detached = pm.detachCopy(book);

			tx.commit();
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
			pm.close();
		}

		return detached;
	}

}
